package com.heima.article.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.FreemarkerService;
import com.heima.file.service.impl.MinIOFileTemplate;
import com.heima.model.common.dtos.ArticleDto;
import com.heima.model.common.pojos.article.ApArticle;
import com.heima.model.common.pojos.article.ApArticleContent;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author Lijx
 * @Date 2024-07-25 14:24
 */
@Service
public class FreemarkerServiceImpl implements FreemarkerService {

    @Autowired
    private ApArticleMapper apArticleMapper;
    @Autowired
    private Configuration configuration;
    @Autowired
    private MinIOFileTemplate minIOFileTemplate;

    /**
     * 生成文章详情页： freemarker + minio
     * @param articleDto
     */
    @Override
    @Async  //标记当前方法为线程异步调用
    public void generateDetailHtml(ArticleDto articleDto) {
        //读取文章内容
        String content = articleDto.getContent();

        StringWriter sw = null;
        InputStream is = null;
        try {
            //获取freemarker模板文件
            Template template = configuration.getTemplate("article.ftl");
            //将文章内容和模板结合
            Map<String,Object> param = new HashMap<>();
            param.put("content", JSONArray.parseArray(content));

            //将模板数据写入内存中
            sw = new StringWriter();
            //合成详情页
            template.process(param,sw);

            //将字符输出流对象转换为字节输出流对象
            is = new ByteArrayInputStream(sw.toString().getBytes(StandardCharsets.UTF_8));
            String filename = articleDto.getId() + ".html";
            //再将模板数据写入到minio中
            String url = minIOFileTemplate.uploadHtmlFile("", filename, is);

            if (StringUtils.isNotEmpty(url)) {
                //查询文章信息
                ApArticle apArticle = apArticleMapper.selectById(articleDto.getId());
                apArticle.setStaticUrl(url);
                //更新文章信息
                apArticleMapper.updateById(apArticle);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //关流
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (sw != null) {
                try {
                    sw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

}

